//
//  STBaseTableView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STBaseTableView.h"
#import "STBaseTableViewCell.h"
@interface STBaseTableView()<UITableViewDataSource ,UITableViewDelegate ,STBaseViewDelegate>

@end
@implementation STBaseTableView
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)sectionArray {
    if (!_sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}
+ (instancetype)instance {
    return [[self alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
}
+ (instancetype)instanceWithFrame:(CGRect)frame {
    return [[self alloc]initWithFrame:frame style:UITableViewStylePlain];
}
+ (instancetype)instanceWithFrame:(CGRect)frame andStyle:(UITableViewStyle)style {
    return [[self alloc]initWithFrame:frame style:style];
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        
        [self commitInit];
        
    }
    return self;
}
- (void)commitInit {
    
    self.dataSource = self;
    
    self.delegate = self;
    
    self.showsVerticalScrollIndicator = false;
    
    self.showsHorizontalScrollIndicator = false;
    
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.backgroundColor = ST_CLEAR_COLOR;
    
    self.bounces = false;
}
#pragma mark ---- table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    STBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld",self.cellType]];
    
    if (!cell) {
        
        cell = [STBaseTableViewCell baseTableViewCell:self.cellType forIndexPath:indexPath];
    }
    cell.mDelegate = self;
    
    cell.indexPath = indexPath;
    
    [cell updateData:self.dataArray[indexPath.row]];
    
    return cell;
}
#pragma mark ---- table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    [self.mDelegate onItemClick:STItemClickTypeItem withData:self.dataArray[indexPath.row] forIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [STBaseTableViewCell heightForCell:self.cellType withData:self.dataArray[indexPath.row] forIndexPath:indexPath];
}
#pragma mark ---- 数据处理

- (void)firstLoadData:(id)data {
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}
- (void)updateData:(id)data {
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
    }
    
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}

- (void)updateData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:data];
    
    [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}
- (void)appendData:(id)data {
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}
- (void)updateFooter:(id)date {
    
}
- (void)updateHeader:(id)data {
    
}
#pragma mark ---- cell delegate

- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        [self.mDelegate onItemClick:itemType withData:data forIndexPath:indexPath];
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        [self.mDelegate onItemClick:STItemClickTypeScroll withData:nil forIndexPath:nil];
        
    }
}

@end
